package com.xf.web.controller.sys;

import java.util.ArrayList;
import java.util.List;

import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.ui.ModelMap;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.PostMapping;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.ResponseBody;

import com.xf.pm.common.PageData;
import com.xf.pm.common.PageForm;
import com.xf.pm.util.ResultMsg;
import com.xf.sys.entity.SysResource;
import com.xf.sys.entity.TreeNode;
import com.xf.sys.service.ISysResourceService;

/**
 * xf-web
 * ResourceController.java
 * author:lb
 * 2017年7月3日
 */
@Controller
@RequestMapping("/sysResource/")
public class SysResourceController {
	
	private final Logger logger =  LoggerFactory.getLogger(this.getClass());
	
	@Autowired
	private ISysResourceService sysResourceService;
	
	/**
	 * 初始化界面
	 * @return
	 */
	@GetMapping("list")
	public String list()
	{
		return "sys/resource/list";
	}
	
	/**
	 * 资源列表查询
	 * @param form
	 * @param sysResource
	 * @return
	 */
	@RequestMapping(value = "selectSysResourceList")
	public @ResponseBody PageData selectSysResourceList(PageForm form,SysResource sysResource)
	{
		return sysResourceService.selectSysResourceList(form,sysResource);
	}
	
	/**
	 * 新增资源
	 * @param sysResource
	 * @return
	 */
	@PostMapping("save")
	public @ResponseBody ResultMsg addSysResource(SysResource sysResource){
		ResultMsg msg=new ResultMsg();
        try {
        	if(null!=sysResource){
        		int result=sysResourceService.insertSysResource(sysResource);
        		if(result==1){
        			msg.setMsg("添加资源成功");
            		msg.setSuccess(true);
        		}else{
        			msg.setMsg("添加资源失败");
            		msg.setSuccess(false);
        		}
            }
		} catch (Exception e) {
			logger.error("添加资源结束异常");
		    e.printStackTrace();
		}
        return msg;
	}
	
	/**
	 * 删除资源
	 * @param ids
	 * @return
	 */
	@PostMapping("remove")
	public @ResponseBody ResultMsg removeSysResource(Integer[] ids){
		ResultMsg msg=new ResultMsg();
        try {
        	if(null!=ids&&ids.length>0){
        		int result=sysResourceService.deleteSysResource(ids);
        		if(result>0){
        			msg.setMsg("删除资源成功");
            		msg.setSuccess(true);
        		}else{
        			msg.setMsg("删除资源失败");
            		msg.setSuccess(false);
        		}
            }
		} catch (Exception e) {
			logger.error("删除资源结束异常");
		    e.printStackTrace();
		}
		return msg;
	}
	
	@PostMapping("update")
	public @ResponseBody ResultMsg updateSysResource(SysResource sysResource){
		ResultMsg msg=new ResultMsg();
        try {
        	if(null!=sysResource){
        		int result=sysResourceService.updateSysResource(sysResource);
        		if(result==1){
            		msg.setMsg("修改资源成功");
            		msg.setSuccess(true);
            	}else{
            		msg.setMsg("修改资源失败");
            		msg.setSuccess(false);
            	}
            }
		} catch (Exception e) {
			logger.error("修改资源结束异常");
		    e.printStackTrace();
		}
        return msg;
	}
	
	/**
	 * 查询单个资源
	 * @param resourceId
	 * @param model
	 * @return
	 */
	@GetMapping("edit")
	public String edit(Integer resourceId, ModelMap model)
	{
		if (resourceId != null)
		{
			model.addAttribute("dto", sysResourceService.selectSysResource(resourceId));
		}
		return "sys/resource/edit";
	}
	
	/**
	 * 获取权限顶部数据
	 * @param menuId
	 * @param roleId
	 * @return
	 */
	@PostMapping("selectSysResource")
	public @ResponseBody List<TreeNode> selectSysResource(Integer menuId,Integer roleId){
		List<TreeNode> list=new ArrayList<TreeNode>();
		try {
			list=sysResourceService.listTree(menuId,roleId);
		} catch (Exception e) {
			logger.error("查询资源异常");
			e.printStackTrace();
		}
		return list;
	}

}
